first = list(map(int,input().split()))
n = len(first)
nums = []
nums.append(first)
for _ in range(n-1):
    info = list(map(int,input().split()))
    nums.append(info)

def bfs(nums):
    if len(nums) <=1:
        return len(nums)
    Q = [(0,0)]
    visited = [(0,0)]
    step = 1
    while Q:
        s = len(Q)
        for i in range(s):
            cur = Q.pop(0)
            if cur == (n - 1, n - 1):
                return step
            m, k = cur[0], cur[1]
            if m == n - 1 and k == n - 1:
                return step
            if m == n - 1:
                if nums[m][k + 1] == 0:
                    Q.append((m, k + 1))
            elif k == n - 1:
                if nums[m + 1][k] == 0:
                    Q.append((m + 1, k))
            else:
                if nums[m + 1][k] == 0:
                    Q.append((m + 1, k))

                if nums[m][k + 1] == 0:
                    Q.append((m, k + 1))
        step += 1
    return -1
print(bfs(nums))


# 0 1 1 1 1
# 0 0 0 1 1
# 1 1 0 1 1
# 1 1 0 0 0
# 1 1 1 1 0




